package com.azzcs.customer.service;


import com.azzcs.customer.dao.TransferDao;
import com.azzcs.customer.entity.Account;
import com.azzcs.spring.anno.Autowired;
import com.azzcs.spring.anno.Service;
import com.azzcs.spring.anno.Transaction;

/**
 * @Author: wzg
 * @Date: 2020/12/13 下午4:13
 */
@Service
public class TransferServiceImpl implements TransferService {

    @Autowired
    private TransferDao transferDao;

    @Override
    @Transaction
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
            Account from = transferDao.queryAccountByCardNo(fromCardNo);
            Account to = transferDao.queryAccountByCardNo(toCardNo);
            int fromMoney = from.getMoney() - money;
            int toMoney = to.getMoney() + money;

            if (fromMoney < 0) {
                throw new Exception("账户余额不足");
            }

            from.setMoney(fromMoney);
            to.setMoney(toMoney);

            transferDao.updateAccountByCardNo(from);
            int i = 1/0;
            transferDao.updateAccountByCardNo(to);
    }
}
